// import store from '/store/index.js'; //vuex  
let server_url = '/api'; //请求地址
let userKey = ' ';
// process.env.NODE_ENV === 'development' ? '192.168.0.1' : '/api' ; //环境配置
function service(options = {}) {
    userKey = uni.getStorageSync('userKey');  //从缓存中获取登录凭证
    options.url = `${server_url}${options.url}`;
    //配置请求头
    options.header = {
        'content-type': 'application/x-www-form-urlencoded',
        'userKey': `${userKey}` //Bearer 
    };

    return new Promise((resolved, rejected) => {
        //成功
        options.success = (res) => {

            if (Number(res.data.code) == 0) {  //请求成功
                resolved(res);
            } else {
                uni.showToast({
                    icon: 'none',
                    duration: 3000,
                    title: `${res.data.msg}`
                });
                rejected(res.msg);//错误
            }

        }
        //错误
        options.fail = (err) => {
            rejected(err); //错误
        }
        uni.request(options);

    });
}
export default service;